package com.link.base.base.product.controller;

import com.link.base.base.product.model.PriceHeadItem;
import com.link.base.base.product.model.PriceList;
import com.link.base.base.product.model.PriceListItem;
import com.link.base.base.product.service.PriceHeadItemService;
import com.link.base.base.product.service.PriceListItemService;
import com.link.base.base.product.service.PriceListService;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.controller.BasicControllerException;
import com.link.core.basic.service.BasicService;
import com.link.core.cllog.LCLogger;
import javax.annotation.Resource;

import com.link.core.basic.service.ServiceException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * <p>价格表列表Controller</p>
 * <p>Description</p>
 * <p>Company</p>
 *
 * @author lc
 * @date 2016年9月22日
 */
@Controller
@RequestMapping("/link/priceHeadItem")
public class PriceHeadItemController extends BasicController<PriceHeadItem> {

    @Resource
    private PriceHeadItemService priceHeadItemService;

    @Resource
    private PriceListService priceListService;

    @Resource
    private PriceListItemService priceListItemService;

    @Override
    public BasicService<PriceHeadItem> getBasicService() throws Exception {
        return priceHeadItemService;
    }

    /**
     * 更新一条记录
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Override
    public Map<String, Object> update(@JsonParam PriceHeadItem entity, HttpSession session,
                                      HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            beforUpdate(entity, request);
            beforUpsert(entity, request);
            checkData(entity, request);
            String base = "base";
            if (!checkExistDataCache(base, session, entity)) {
                throw new ServiceException("PUBLIC-002");
            }
            PriceList priceList = new PriceList();
            priceList.setId(entity.getId());
            priceList.setPriceListName(entity.getPriceListName());
            priceList.setEffStartDate(entity.getEffStartDate());
            priceList.setEffEndDate(entity.getEffEndDate());
            priceList.setOrgId(entity.getOrgId());
            priceList.setActivated(entity.getActivated());
            priceList.setComments(entity.getComments());
            priceListService.update(priceList);
            PriceListItem priceListItem = new PriceListItem();
            priceListItem.setId(entity.getItemId());
            priceListItem.setHeadId(entity.getId());
            priceListItem.setProdId(entity.getProdId());
            priceListItem.setPrice(entity.getPrice());
            priceListItem.setPromoPrice(entity.getPromoPrice());
            priceListItem.setEffStartDate(entity.getEffStartDateItem());
            priceListItem.setEffEndDate(entity.getEffEndDateItem());
            priceListItem.setComments(entity.getCommentsItem());
            priceListItemService.update(priceListItem);

            PriceHeadItem newEntry = getBasicService().queryById(entity);
            result.put("newRow", newEntry);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 删除一条记录
     */
    @Override
    @RequestMapping(value = "/deleteById")
    @ResponseBody
    public Map<String, Object> deleteById(@JsonParam PriceHeadItem entity, HttpSession session,
                                          HttpServletRequest request, HttpServletResponse response) {
        LCLogger.info().withMessageKey("deleteById")
                .withMessage("删除一条列表信息...").flush();
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            beforDelete(entity, request);
            checkData(entity, request);
            String base = "base";
            if (!checkExistDataCache(base, session, entity)) {
                throw new ServiceException("PUBLIC-031");
            }

            PriceListItem priceListItem = new PriceListItem();
            priceListItem.setId(entity.getItemId());
            //删除行信息
            priceListItemService.deleteById(priceListItem);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 插入或更新前做操作
     */
    public void beforUpsert(PriceList entity, HttpServletRequest request) throws Exception {
        SimpleDateFormat dfFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date sDate2 = dfFormat.parse(entity.getEffStartDate());
        //有效期结束时间不为空
        if (entity.getEffEndDate() != null && !"".equals(entity.getEffEndDate())) {
            Date eDate2 = dfFormat.parse(entity.getEffEndDate());
            //判断有效期开始时间和有效期结束时间大小关系
            if (eDate2.getTime() < sDate2.getTime()) {
                throw new BasicControllerException("价格表的有效结束时间不能比有效开始时间早，请重新编辑！");
            }
        }
    }


}
